package com.vim.modules.mall.controller;

import com.github.pagehelper.PageInfo;
import com.vim.common.base.BaseController;
import com.vim.modules.mall.model.base.Page;
import com.vim.modules.mall.model.mallGoods.MallGoods;
import com.vim.modules.mall.model.mallGoods.MallGoodsResource;
import com.vim.modules.mall.model.mallSys.MallSysTask;
import com.vim.modules.mall.service.MallSysTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @作者 Administrator
 * @时间 2020-10-06 11:19
 * @版本 1.0
 */
@Controller
@RequestMapping(value = "/mall/sys/task")
public class MallSysTaskController extends BaseController{

    @Autowired
    private MallSysTaskService mallSysTaskService;

    /**
     * 任务列表页面
     */
    @RequiresPermissions(value = "mall:sys:task:list")
    @RequestMapping(value = "list")
    public String list(){
        return "mall/sys/sysTaskList";
    }

    /**
     * 任务列表数据
     * @param pageNo    页码
     * @param pageSize  每页数量
     */
    @RequiresPermissions(value = "mall:sys:task:list")
    @RequestMapping(value = "/dataList")
    @ResponseBody
    public PageInfo<MallSysTask> dataList(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        return mallSysTaskService.taskList(new Page(pageNo,pageSize));
    }

    /**
     * 任务编辑页面
     * @param id 任务id
     */
    @RequiresPermissions(value = "mall:sys:task:form")
    @RequestMapping(value = "/form")
    public String form(@RequestParam(value = "id", required = false) Long id, Model model){
        model.addAttribute("task", null != id ? mallSysTaskService.taskInfo(id) : new MallSysTask());
        return "mall/sys/sysTaskForm";
    }

    /**
     * 任务保存
     */
    @RequiresPermissions(value = "mall:sys:task:form")
    @RequestMapping(value = "/save")
    @ResponseBody
    public ResponseEntity save(MallSysTask task){
        return new ResponseEntity<>(success(mallSysTaskService.sysTaskSave(task)), HttpStatus.OK);
    }

    /**
     * 修改任务状态
     * @param id      任务id
     * @param status 状态（0停止 1启用）
     */
    @RequiresPermissions(value = "mall:sys:task:ctrl")
    @RequestMapping(value = "/ctrl")
    @ResponseBody
    public ResponseEntity ctrl(
            @RequestParam(value = "id") Long id,
            @RequestParam(value = "status") String status
    ){
        mallSysTaskService.sysTaskCtrl(id, status);
        return new ResponseEntity(success(), HttpStatus.OK);
    }

}
